/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Nota.java
 *
 * Created on 18 Apr 12, 4:11:39
 */

package kasirmain;

import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Ahmad 'ANS' Syuhada
 */
public class Nota extends javax.swing.JFrame {

    /** Creates new form Nota */
    public Nota(Transaksi tr) {
        initComponents();
        //set label
        lblTrans.setText("No Transaksi : "+tr.getNotrans());
        lblTgl.setText("Tanggal : "+tr.getDate());
        lblMeja.setText("Meja : "+tr.getMeja());
        //set tabel
        DefaultTableModel tempmo = (DefaultTableModel)tblNota.getModel();
        int ctbl = tempmo.getRowCount();
        ArrayList<Pesanan> temppes = tr.getPesanan();
        int cpes = temppes.size();
        //add
        if (ctbl<cpes){
            for (int j=ctbl; j<cpes; j++){
                Pesanan tpes = temppes.get(j);
                tempmo.insertRow(ctbl, new Object[]{"","","",""});
            }
        }else if (ctbl>cpes){   //del
            for (int j=ctbl; j>cpes; j--){
                tempmo.removeRow(j-1);
            }
        }
        //update
        for (int j=0; j<cpes; j++){
            Pesanan tpes = temppes.get(j);
            tempmo.setValueAt(tpes.getID(), j, 0);
            tempmo.setValueAt(tpes.getType(), j, 1);
            tempmo.setValueAt(tpes.getQty(), j, 2);
            tempmo.setValueAt(100, j, 3);
        }
        tblNota.setModel(tempmo);
        //set total
        int total=0;

        for (int t=0; t<cpes; t++){
            total += (Integer) tblNota.getValueAt(t, 3);
        }
        lblTotal.setText(total+"");
        txtBayar.addKeyListener
        (new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_ENTER) {
                  Toolkit.getDefaultToolkit().beep();
                  System.out.println("ENTER pressed");
                  int proc = Integer.parseInt(txtBayar.getText()) - Integer.parseInt(lblTotal.getText());
                  lblKembali.setText(proc+"");
                  if (proc>=0) btnCetak.setEnabled(true);
            }
           }
         }
        );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTgl = new javax.swing.JLabel();
        lblMeja = new javax.swing.JLabel();
        lblTrans = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblNota = new javax.swing.JTable();
        jlabel = new javax.swing.JLabel();
        txtBayar = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jlabel2 = new javax.swing.JLabel();
        lblTotal = new javax.swing.JLabel();
        lblKembali = new javax.swing.JLabel();
        btnCetak = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Nota Transaksi");
        setAlwaysOnTop(true);
        setMaximizedBounds(new java.awt.Rectangle(0, 0, 400, 464));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        lblTgl.setText("lblTgl");

        lblMeja.setText("Meja : ");

        lblTrans.setText("No Transaksi : ");

        tblNota.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Nama", "Qty", "Sub-total"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblNota);
        tblNota.getColumnModel().getColumn(0).setResizable(false);
        tblNota.getColumnModel().getColumn(1).setResizable(false);
        tblNota.getColumnModel().getColumn(2).setResizable(false);
        tblNota.getColumnModel().getColumn(3).setResizable(false);

        jlabel.setText("Total : ");

        jLabel5.setText("Bayar : ");

        jlabel2.setText("Kembalian :");

        btnCetak.setText("Cetak Nota");
        btnCetak.setEnabled(false);
        btnCetak.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCetakActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblTgl)
                    .addComponent(lblMeja)
                    .addComponent(lblTrans)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnCetak)
                                .addGap(144, 144, 144)
                                .addComponent(jlabel))
                            .addComponent(jlabel2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblKembali)
                            .addComponent(lblTotal)
                            .addComponent(txtBayar, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTgl)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblMeja)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblTrans)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jlabel)
                            .addComponent(lblTotal))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtBayar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jlabel2)
                            .addComponent(lblKembali)))
                    .addComponent(btnCetak))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        Main.utama.setEnabled(true);
    }//GEN-LAST:event_formWindowClosing

    private void btnCetakActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCetakActionPerformed
        // TODO add your handling code here:
        System.out.println("Cetak nota clicked");
        //save ke sql
        //print nota
        Main.utama.setEnabled(true);
        this.dispose();
    }//GEN-LAST:event_btnCetakActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Nota(new Transaksi()).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCetak;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel jlabel;
    private javax.swing.JLabel jlabel2;
    private javax.swing.JLabel lblKembali;
    private javax.swing.JLabel lblMeja;
    private javax.swing.JLabel lblTgl;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JLabel lblTrans;
    private javax.swing.JTable tblNota;
    private javax.swing.JTextField txtBayar;
    // End of variables declaration//GEN-END:variables

}
